package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.SelPicActivity;
import flc.ast.adapter.ConstructAdapter;
import flc.ast.adapter.RecordAdapter;
import flc.ast.bean.MyConstructBean;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.FragmentConstructBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ConstructFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ConstructFragment extends BaseNoModelFragment<FragmentConstructBinding> {

    private ConstructAdapter constructAdapter;
    private RecordAdapter recordAdapter;
    private List<MyRecordBean> listShow=new ArrayList<>();
    private List<MyRecordBean> listAll=new ArrayList<>();



    @Override
    protected int onCreate() {
        return R.layout.fragment_construct;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        GridLayoutManager layoutManager=new GridLayoutManager(mContext,2);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mDataBinding.rvConstructList.setLayoutManager(layoutManager);
        constructAdapter=new ConstructAdapter();
        mDataBinding.rvConstructList.setAdapter(constructAdapter);
        constructAdapter.setOnItemClickListener(this);
        mDataBinding.rvRecordList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        recordAdapter=new RecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.addChildClickViewIds(R.id.ivRecordItemDelete);
        recordAdapter.addChildClickViewIds(R.id.ivRecordItemCancel);
        recordAdapter.setOnItemChildClickListener(this);
        recordAdapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                recordAdapter.getItem(position).setSelected(true);
                recordAdapter.notifyItemChanged(position);
                return false;
            }
        });
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()){
            case R.id.ivRecordItemDelete:
                MyRecordBean bean=recordAdapter.getItem(position);
                bean.setSelected(false);
                listAll.remove(bean);
                AppSpUtils.saveRecordList(listAll);
                getRecord();
                break;
            case R.id.ivRecordItemCancel:
                recordAdapter.getItem(position).setSelected(false);
                recordAdapter.notifyItemChanged(position);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        SelPicActivity.selPosition=position;
        startActivity(SelPicActivity.class);
    }

    @Override
    protected void initData() {
        List<MyConstructBean> list=new ArrayList<>();
        list.add(new MyConstructBean(R.drawable.aa01));
        list.add(new MyConstructBean(R.drawable.aa02));
        list.add(new MyConstructBean(R.drawable.aa03));
        list.add(new MyConstructBean(R.drawable.aa04));
        list.add(new MyConstructBean(R.drawable.aa05));
        list.add(new MyConstructBean(R.drawable.aa06));
        list.add(new MyConstructBean(R.drawable.aa07));
        list.add(new MyConstructBean(R.drawable.aa08));
        list.add(new MyConstructBean(R.drawable.aa09));
        list.add(new MyConstructBean(R.drawable.aa10));
        list.add(new MyConstructBean(R.drawable.aa11));
        list.add(new MyConstructBean(R.drawable.aa12));
        list.add(new MyConstructBean(R.drawable.aa13));
        list.add(new MyConstructBean(R.drawable.aa14));
        list.add(new MyConstructBean(R.drawable.aa15));
        list.add(new MyConstructBean(R.drawable.aa16));
        list.add(new MyConstructBean(R.drawable.aa17));
        list.add(new MyConstructBean(R.drawable.aa18));
        list.add(new MyConstructBean(R.drawable.aa19));
        list.add(new MyConstructBean(R.drawable.aa20));
        list.add(new MyConstructBean(R.drawable.aa21));
        constructAdapter.setList(list);
    }

    @Override
    public void onResume() {
        super.onResume();
        getRecord();
    }

    private void getRecord(){
        listShow.clear();
        listAll.clear();
        List<MyRecordBean> listSp = AppSpUtils.getRecordList();
        if (listSp != null && listSp.size() > 0) {
            for (MyRecordBean myRecordBean : listSp) {
                if (myRecordBean.getType()==1){
                    listShow.add(myRecordBean);
                }
            }
            listAll.addAll(listSp);
            recordAdapter.setList(listShow);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
//            mDataBinding.llCollectNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
//            mDataBinding.llCollectNoData.setVisibility(View.VISIBLE);
        }
    }

}